Skip to content

下载文件 - HttpDownloadFile

函数简介

下载文件,支持断点续传与进度回调。

接口名称

HttpDownloadFile

DLL调用

c
int32_t HttpDownloadFile(int64_t instance, const char* url, const char* save_path, DownloadCallback callback, int64_t user_data);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
url字符串完整URL。
save_path字符串本地保存路径。
callbackDownloadCallback回调函数,用于接收下载进度。
user_data长整数型传给callback的用户数据。

使用场景

场景callback说明
静默下载NULL / nullptr不需要进度显示
显示进度条注册回调current/total 计算百分比
限速监控注册回调speed 为当前字节/秒

回调在下载过程中 多次触发total=0 表示服务器未返回 Content-Length。

回调函数类型定义

c
void DownloadCallback(int64_t current, int64_t total, int64_t speed, int64_t user_data);
参数名类型说明
current长整数型已下载字节数
total长整数型总字节数(0表示未知)
speed长整数型当前下载速度(字节/秒)
user_data长整数型由调用方传入的用户数据

错误码

错误码说明
0下载成功
-1无效的URL
-2无法创建文件
-3无法写入文件
-4连接失败
-5超时
-6DNS解析失败
-7SSL/TLS错误
-404404文件不存在
-403403禁止访问
-500500服务器错误
-502502网关错误
-503503服务不可用
-999未知错误

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// callback=nullptr 表示不需要进度回调
int ret = ola.HttpDownloadFile(
    "https://example.com/file.zip",
    "D:\\downloads\\file.zip",
    nullptr, 0
);
if (ret == 0) {
    // 下载成功,支持断点续传
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
int ret = ola.HttpDownloadFile("https://example.com/file.zip", @"D:\downloads\file.zip", null, 0);
if (ret == 0) { /* 下载成功 */ }
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
ret = ola.HttpDownloadFile("https://example.com/file.zip", r"D:\downloads\file.zip", None, 0)
if ret == 0:
    pass  # 下载成功
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
int ret = ola.HttpDownloadFile("https://example.com/file.zip", "D:\\downloads\\file.zip", null, 0);
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
ret := ola.HttpDownloadFile("https://example.com/file.zip", "D:\\downloads\\file.zip", nil, 0)
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let ret = ola.http_download_file("https://example.com/file.zip", "D:\\downloads\\file.zip", None, 0);
cpp
var ola = com("OlaPlug.OlaSoft")
var ret = ola.HttpDownloadFile("https://example.com/file.zip", "D:\\downloads\\file.zip", 0, 0)
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
ret = ola.HttpDownloadFile("https://example.com/file.zip", "D:\downloads\file.zip", 0, 0)
text
.局部变量 ola, OLAPlug
ola.创建 ()
ret = ola.HttpDownloadFile ("https://example.com/file.zip", "D:\downloads\file.zip", 0, 0)
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var ret = ola.HttpDownloadFile("https://example.com/file.zip", "D:\downloads\file.zip", null, 0);
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
整数 ret = ola.HttpDownloadFile("https://example.com/file.zip", "D:\downloads\file.zip", 0, 0)
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
int ret = ola.HttpDownloadFile("https://example.com/file.zip", "D:\\downloads\file.zip", nullptr, 0);

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
int ret = HttpDownloadFile(instance, "https://example.com/file.zip", "D:\\downloads\\file.zip", 0, 0);
// ret==0 成功,<0 见错误码表
csharp
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
int ret = HttpDownloadFile(instance, "https://example.com/file.zip", "D:\\downloads\\file.zip", 0, 0);
// ret==0 成功,<0 见错误码表
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
int ret = HttpDownloadFile(instance, "https://example.com/file.zip", "D:\\downloads\\file.zip", 0, 0);
// ret==0 成功,<0 见错误码表

返回值

返回值说明
0成功。
< 0失败,错误码与相关接口一致。

注意事项

项目说明
支持断点续传支持断点续传,如果文件已部分下载,会自动从断点处继续。
回调函数在下载过程中会被多次调用回调函数在下载过程中会被多次调用。
如果不需要进度回调如果不需要进度回调,可以传NULL。
支持HTTP和HTTPS协议支持HTTP和HTTPS协议。